# -*- coding: utf-8 -*-
# @Time : 2022/6/15 上午11:53
# @Author : wyf
# @FileName: selenium截图.py
# @Email: yunfei.wang@innvote.com
from io import BytesIO
from PIL import Image
from selenium import webdriver
from selenium.webdriver.common.by import By

browser = webdriver.Chrome()
# 将浏览器最大化显示
browser.maximize_window()

url = 'https://pic.netbian.com/4kmeinv/index.html'
browser.get(url)

img = browser.find_element(By.XPATH, '//*[@id="main"]/div[3]/ul/li[1]/a/img')
print(img)
location = img.location  # 初始坐标(左上角) # {'x': 0, 'y': 164}
size = img.size    # 高和宽  # {'height': 260, 'width': 390}
print("location", location)
print("size", size)

# 上   下      左     右
top, bottom, left, right = location["y"], location["y"] + size["height"], location["x"], location["x"] + size["width"]
print(top, bottom, left, right)

#
screenshot = browser.get_screenshot_as_png()  # 截取整个浏览显示页面
img = Image.open(BytesIO(screenshot))
#           (左上角x, 左上角y, 右下角x, 右下角y)
img2 = img.crop((left, top, right, bottom))

img.save("11.png")
img2.save("22.png")

browser.quit()
